<template>
  <div class="login-container">
    <!-- 背景动画 -->
    <div class="area">
      <ul class="circles">
        <li v-for="n in 10" :key="n"></li>
      </ul>
    </div>

    <div class="login-box">
      <div class="login-card">
        <div class="left-panel">
          <div class="welcome-text">
            <div class="logo-container">
              <img src="../assets/logo.svg" alt="Logo" class="logo-image" />
            </div>
            <h2>欢迎回来</h2>
            <p>AI智能出题系统</p>
            <div class="features">
              <div class="feature-item">
                <el-icon><Document /></el-icon>
                <span>智能出题</span>
              </div>
              <div class="feature-item">
                <el-icon><ChatLineRound /></el-icon>
                <span>题库管理</span>
              </div>
              <div class="feature-item">
                <el-icon><ChatLineRound /></el-icon>
                <span>快速组卷</span>
              </div>
              <div class="feature-item">
                <el-icon><DataAnalysis /></el-icon>
                <span>可视化分析</span>
              </div>
            </div>
          </div>
        </div>
        <div class="right-panel">
          <h3 class="login-title">用户登录</h3>
          <el-form 
            :model="loginForm" 
            :rules="rules" 
            ref="loginFormRef"
            class="login-form"
            size="large"
          >
            <el-form-item prop="username">
              <el-input 
                v-model="loginForm.username" 
                placeholder="请输入用户名"
              >
                <template #prefix>
                  <el-icon class="input-icon"><User /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input 
                v-model="loginForm.password" 
                type="password" 
                placeholder="请输入密码"
                show-password
              >
                <template #prefix>
                  <el-icon class="input-icon"><Lock /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            
            <div class="options-row">
              <el-checkbox v-model="rememberMe">记住我</el-checkbox>
              <el-button type="text" @click="forgotPassword" class="forgot-btn">忘记密码？</el-button>
            </div>

            <el-form-item>
              <el-button 
                type="primary" 
                @click="handleLogin"
                class="login-button"
                :loading="loading"
              >
                <el-icon v-if="!loading"><Right /></el-icon>
                <span>{{ loading ? '登录中...' : '登录' }}</span>
              </el-button>
            </el-form-item>

            <div class="divider">
              <span class="divider-text">其他登录方式</span>
            </div>

            <div class="social-login">
              <el-button circle class="social-icon">
                <el-icon><Platform /></el-icon>
              </el-button>
              <el-button circle class="social-icon">
                <el-icon><Message /></el-icon>
              </el-button>
              <el-button circle class="social-icon">
                <el-icon><ChatDotRound /></el-icon>
              </el-button>
            </div>

            <div class="register-link">
              还没有账号？
              <el-button 
                type="text" 
                @click="switchToRegister"
                class="register-btn"
              >
                立即注册
              </el-button>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, reactive, ref } from 'vue';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';
import { 
  User, 
  Lock, 
  Right,
  Platform,
  Message,
  ChatDotRound,
  Document,
  ChatLineRound,
  DataAnalysis
} from '@element-plus/icons-vue';
import { request } from '../api/request';

const router = useRouter();
const loginFormRef = ref(null);
const loading = ref(false);
const rememberMe = ref(false);

const loginForm = reactive({
  username: '',
  password: '',
});

// 获取用户信息
const getUserInfo = async () => {
  try {
    const storedUser = localStorage.getItem('user')
    if (storedUser) {
      const user = JSON.parse(storedUser)
      loginForm.username = user.username
      loginForm.email = user.email
    }
  } catch (error) {
    ElMessage.error('获取用户信息失败')
  }
}
onMounted(() => {
  getUserInfo()
})
const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, message: '用户名长度不能小于3个字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6个字符', trigger: 'blur' },
  ],
});

const handleLogin = async () => {
  if (!loginFormRef.value) return;
  
  try {
    await loginFormRef.value.validate();
    loading.value = true;
    
    const response = await request.post('/user/login', {
      username: loginForm.username,
      password: loginForm.password
    });
    
    if (response.success) {
      ElMessage.success({
        message: response.message || '登录成功，正在跳转...',
        duration: 1500,
        showClose: true,
      });
      
      // 存储token和用户信息
      if (response.user) {
        localStorage.setItem('token', response.user.id);
        localStorage.setItem('user', JSON.stringify(response.user));
      }     

   
      
      setTimeout(() => {
        router.push('/home');
      }, 1500);
    } else {
      ElMessage.error({
        message: response.message || '登录失败',
        duration: 2000,
        showClose: true,
      });
    }
  } catch (error) {
    ElMessage.error({
      message: error.message || '登录失败，请重试',
      duration: 2000,
      showClose: true,
    });
  } finally {
    loading.value = false;
  }
};

const switchToRegister = () => {
  router.push('/register');
};

const forgotPassword = () => {
  ElMessage.info({
    message: '请联系管理员重置密码',
    duration: 2000,
    showClose: true,
  });
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  padding: 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
}

/* 背景动画 */
.area {
  position: absolute;
  width: 100%;
  height: 100%;
}

.circles {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  margin: 0;
  padding: 0;
}

.circles li {
  position: absolute;
  display: block;
  list-style: none;
  width: 20px;
  height: 20px;
  background: rgba(255, 255, 255, 0.2);
  animation: animate 25s linear infinite;
  bottom: -150px;
  border-radius: 50%;
}

.circles li:nth-child(1) {
  left: 25%;
  width: 80px;
  height: 80px;
  animation-delay: 0s;
}

.circles li:nth-child(2) {
  left: 10%;
  width: 20px;
  height: 20px;
  animation-delay: 2s;
  animation-duration: 12s;
}

.circles li:nth-child(3) {
  left: 70%;
  width: 20px;
  height: 20px;
  animation-delay: 4s;
}

.circles li:nth-child(4) {
  left: 40%;
  width: 60px;
  height: 60px;
  animation-delay: 0s;
  animation-duration: 18s;
}

.circles li:nth-child(5) {
  left: 65%;
  width: 20px;
  height: 20px;
  animation-delay: 0s;
}

@keyframes animate {
  0% {
    transform: translateY(0) rotate(0deg);
    opacity: 1;
    border-radius: 0;
  }
  100% {
    transform: translateY(-1000px) rotate(720deg);
    opacity: 0;
    border-radius: 50%;
  }
}

.login-box {
  width: 1000px;
  min-height: 600px;
  height: auto;
  perspective: 1000px;
  position: relative;
  z-index: 1;
}

.login-card {
  display: flex;
  height: 100%;
  width: 100%;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(10px);
  background: rgba(255, 255, 255, 0.9);
  transition: all 0.3s ease;
}

.left-panel {
  flex: 1;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: white;
  position: relative;
  overflow: hidden;
}

.logo-container {
  margin-bottom: 30px;
}

.logo-image {
  width: 100px;
  height: 100px;
  object-fit: contain;
}

.welcome-text {
  text-align: center;
  position: relative;
  z-index: 1;
}

.welcome-text h2 {
  font-size: 2.5em;
  margin-bottom: 20px;
  font-weight: bold;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
}

.welcome-text p {
  font-size: 1.2em;
  opacity: 0.9;
  margin-bottom: 30px;
}

.features {
  display: flex;
  flex-direction: column;
  gap: 20px;
  margin-top: 40px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 1.1em;
  opacity: 0.9;
  transition: all 0.3s ease;
}

.feature-item:hover {
  transform: translateX(10px);
  opacity: 1;
}

.right-panel {
  flex: 1;
  background: white;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.login-title {
  text-align: center;
  font-size: 2em;
  color: #333;
  margin-bottom: 40px;
  font-weight: bold;
}

.login-form {
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
}

.input-icon {
  color: #666;
  font-size: 18px;
}

.options-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 20px 0;
}

.forgot-btn {
  color: #666;
  transition: all 0.3s ease;
}

.forgot-btn:hover {
  color: #764ba2;
  transform: translateX(5px);
}

.login-button {
  width: 100%;
  height: 45px;
  font-size: 16px;
  border-radius: 25px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  margin: 20px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(118, 75, 162, 0.4);
}

.divider {
  display: flex;
  align-items: center;
  margin: 20px 0;
  color: #666;
}

.divider::before,
.divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: #ddd;
}

.divider-text {
  padding: 0 15px;
  font-size: 14px;
  color: #666;
}

.social-login {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin: 20px 0;
}

.social-icon {
  width: 40px;
  height: 40px;
  border: 1px solid #ddd;
  transition: all 0.3s ease;
}

.social-icon:hover {
  background: #f5f5f5;
  transform: translateY(-2px);
}

.register-link {
  text-align: center;
  margin-top: 20px;
  color: #666;
}

.register-btn {
  color: #764ba2;
  font-weight: bold;
  transition: all 0.3s ease;
}

.register-btn:hover {
  transform: translateX(5px);
}

@media (max-width: 768px) {
  .login-box {
    width: 95%;
    min-height: auto;
  }
  
  .login-card {
    flex-direction: column;
  }
  
  .left-panel {
    padding: 30px;
    min-height: 200px;
  }
  
  .right-panel {
    padding: 30px 20px;
  }

  .login-form {
    max-width: 100%;
  }
  
  .features {
    display: none;
  }
}

/* 添加输入框动画效果 */
.el-input :deep(.el-input__wrapper) {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.el-input :deep(.el-input__wrapper:hover),
.el-input :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 4px 15px rgba(118, 75, 162, 0.1);
  transform: translateY(-2px);
}

/* 添加表单项动画 */
.el-form-item {
  transition: all 0.3s ease;
  transform: translateY(0);
}

.el-form-item:hover {
  transform: translateY(-2px);
}
</style>